package com.jx.flowershopmanager.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jx.flowershopmanager.R;
import com.jx.flowershopmanager.base.BaseActivity;
import com.jx.flowershopmanager.entity.User;
import com.jx.flowershopmanager.home.HomeActivity;
import com.jx.flowershopmanager.network.RetrofitCallback;
import com.jx.flowershopmanager.network.RetrofitUtil;
import com.jx.flowershopmanager.util.JsonToMapUtil;
import com.jx.flowershopmanager.util.SPUtils;
import com.jx.flowershopmanager.widget.ClearEditText;
import com.jx.flowershopmanager.widget.SubmitButton;

import java.util.HashMap;
import java.util.Map;


public class LoginActivity extends BaseActivity {

    /// 登录按钮
    private SubmitButton btnLogin;
    /// 用户名输入框
    private EditText userEdit;
    /// 密码输入框
    private EditText passwordEdit;

    public LoginActivity() {
    }


    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        btnLogin = findViewById(R.id.btn_login_commit);
        userEdit = findViewById(R.id.et_login_phone);
        passwordEdit = findViewById(R.id.et_login_password);
        btnLogin.setOnClickListener(v -> {
            login();
        });
    }


    @Override
    protected void initData() {

    }

    /// 登录事件
    private void login() {
        Map<String, String> map = new HashMap<>(16);
        if (userEdit.getText() == null) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
        } else if (passwordEdit.getText() == null) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
        } else {
            map.put("name", userEdit.getText().toString());
            map.put("password", passwordEdit.getText().toString());
            RetrofitUtil.post("/user/login", map, new RetrofitCallback() {
                @Override
                public void onSuccess(String resultJsonString) {
                    Gson gson = new Gson();
                    User user = gson.fromJson(resultJsonString, User.class);
                    if (user.isSuccess()) {
                        SPUtils spUtils = new SPUtils(LoginActivity.this, "user");
                        spUtils.putString("token", user.getUserData().getToken());
                        spUtils.putString("id", user.getUserData().getId());
                        spUtils.putString("name", user.getUserData().getName());
                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(intent);
                        finish();
                        btnLogin.showSucceed();
                    } else {
                        Toast.makeText(LoginActivity.this, user.getMsg(), Toast.LENGTH_SHORT).show();
                        btnLogin.showError(3000);
                    }
                }

                @Override
                public void onError(Throwable t) {
                    Log.i("TAG", t.getMessage());
                    btnLogin.showError(3000);
                }
            });
        }
    }
}